/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.composedui.views.elements;

import javax.microedition.lcdui.Graphics;

import com.nokia.example.composedui.model.DataItem;
import com.nokia.example.composedui.utils.ImageUtils;

public class IconListItem
    extends GridItem
{
    // Constants
    private static final int MARGIN = 5;
    private static final int TEXT_COLOR_NOT_SELECTED = 0x595959;
    private static final int TEXT_COLOR_SELECTED = 0xfdfefe;
    private static final int HILIGHT_COLOR = 0x29a7cc;
    private static final int BACKGROUND_COLOR = 0xf4f4f4;

    // Members
    private int lastX = 0;
    private int lastY = 0;

    /**
     * Constructor.
     * @param dataItem The data item associated with this list item.
     */
    public IconListItem(DataItem dataItem, final int width, final int height) {
        super();
        
        this.dataItem = dataItem;
        
        if (this.dataItem != null) {
            imageUri = this.dataItem.getImageFileName();
            text = this.dataItem.getName();
        }
        
        this.width = width;
        this.height = height;
        ImageUtils.getImageAsync(imageUri, height, height, this);
    }

    /**
     * @see GridItem#paint(Graphics, int, int)
     */
    public void paint(Graphics graphics, int width, int height) {
        // Paint the background
        if (highlight) {
            graphics.setColor(HILIGHT_COLOR);
        }
        else {
            graphics.setColor(BACKGROUND_COLOR);
        }
        
        graphics.fillRect(0, 0, width, height);
        
        // Paint the icon. The method comes from GridItem.
        paint(graphics, 0, 0, height, height);
        
        // Paint the text
        if (highlight) {
            graphics.setColor(TEXT_COLOR_SELECTED);
        }
        else {
            graphics.setColor(TEXT_COLOR_NOT_SELECTED);
        }
        
        graphics.drawString(text,
                            height + MARGIN,
                            (height - graphics.getFont().getHeight()) / 2,
                            Graphics.TOP | Graphics.LEFT);
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#pointerPressed(int, int)
     */
    protected void pointerPressed(int x, int y) {
        lastX = x;
        lastY = y;
        highlight = true;
        repaint();
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#pointerDragged(int, int)
     */
    protected void pointerDragged(int x, int y) {
        if (Math.abs(lastX - x) > GridLayout.JITTER_THRESHOLD
            || Math.abs(lastY - y) > GridLayout.JITTER_THRESHOLD)
        {
            // Too much jitter. Lose the focus.
            highlight = false;
            repaint();
        }
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#pointerReleased(int, int)
     */
    protected void pointerReleased(int x, int y) {
        if (highlight) {
            this.notifyStateChanged();
            highlight = false;
            repaint();
        }
    }

    /**
     * @see GridItem#setSize(int, int)
     */
    public void setSize(final int width, final int height) {
        this.width = width;
        this.height = height;
        
        // Resize the image
        ImageUtils.getImageAsync(imageUri, height, height, this);
    }
}
